package apigenerator;

import java.lang.reflect.Method;

import org.aspectj.weaver.tools.PointcutExpression;
import org.aspectj.weaver.tools.PointcutParser;
import org.aspectj.weaver.tools.ShadowMatch;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.core.annotation.Order;

/**
 * @author 1203056
 * @version Id: AspectjExpressionTypeFilter.java, v 0.1 2021-09-07 14:15 1203056 Exp $
 */
@Order(2)
public class AspectjExpressionMethodFilter implements MethodFilter {

    private PointcutParser pointcutParser     = PointcutParser
        .getPointcutParserSupportingSpecifiedPrimitivesAndUsingContextClassloaderForResolution(PointcutParser.getAllSupportedPointcutPrimitives());
    PointcutExpression     pointcutExpression = null;

    public AspectjExpressionMethodFilter() {

        //        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        //TODO 是否线程安全
        pointcutExpression = pointcutParser.parsePointcutExpression("execution(* apigenerator.*Facade.*(..))");
    }

    @Override
    public boolean match(Method type) {
        ShadowMatch shadowMatch = pointcutExpression.matchesMethodExecution(type);
        if (shadowMatch.alwaysMatches()) {
            return true;
        } else if (shadowMatch.neverMatches()) {
            return false;
        } else {
            return false;
        }
    }
}
